package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.enums.MediaEnum;
import com.heima.common.minio.service.MinIOService;
import com.heima.common.util.UserThreadLocalUtil;
import com.heima.media.dto.WmMaterialDto;
import com.heima.media.entity.WmMaterial;
import com.heima.media.mapper.WmMaterialMapper;
import com.heima.media.service.IWmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-05-16
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements IWmMaterialService {


    @Autowired
    private MinIOService minIOService;


    /**
     * 保存素材  1.上传素材文件到MinIO  2.保存素材记录
     *
     * @param file 上传文件
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<WmMaterial> uploadPicture(MultipartFile file) {
        User user = UserThreadLocalUtil.get();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //1.将得到文件上传到MinIO
        String upload = minIOService.upload(file);
        //2.构建素材对象，保存素材记录
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(user.getUserId());
        wmMaterial.setUrl(upload);
        wmMaterial.setType(MediaEnum.MEDIA_MATERIAL_TYPE_IMG.getVal());
        wmMaterial.setIsCollection(false);
        this.save(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }


    /**
     * 分页查询当前用户素材列表
     *
     * @param dto 条件
     * @return
     */
    @Override
    public ResponseResult listByCollection(WmMaterialDto dto) {
        User user = UserThreadLocalUtil.get();
        //1.构建查询条件
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmMaterial::getUserId, user.getUserId());
        if (dto.getIsCollection() != null) {
            queryWrapper.eq(WmMaterial::getIsCollection, dto.getIsCollection());
        }
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);
        //2.构建分页对象
        IPage<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());

        //3.执行查询
        page = this.page(page, queryWrapper);

        //4.封装分页结果
        return new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());
    }
}
